package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;


public class IO {
	
	public static byte[] getFileBytes(File theFile) throws FileNotFoundException, FileToBigException{
		
		
		byte[] fileContent;
		long fl = theFile.length();
		int fli = (int) theFile.length();
		if (fl == fli) { //ensures that the file's length isn't above the integer limit
			fileContent = new byte[(int) theFile.length()];
			RandomAccessFile rf = new RandomAccessFile(theFile, "r");
			try {
				rf.readFully(fileContent);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else{
			throw new FileToBigException();
		}
		
		return fileContent;
	}
	
	public static String getFileString(File file) throws FileNotFoundException, FileToBigException{
		return new String(getFileBytes(file));
	}
	
	public static String getFileString(String fname) throws FileNotFoundException, FileToBigException{
		return getFileString(new File(fname));
	}
	
	public static byte[] getFileBytes(String fname) throws FileNotFoundException, FileToBigException{
		File f = new File(fname);
		return getFileBytes(f);
	}
	
	public static File findFile(String fname) throws Exception404{
		File f = new File(fname);
		if(f.exists()){
			return f;
		}//TODO assumption code
		else{
			throw new Exception404();
		}
	}
	
}
